// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { TagData } from "./tagsCtrl";

const {ccclass, property, executeInEditMode, menu} = cc._decorator;

@ccclass
@executeInEditMode
@menu('扩展组件/TagNode')
export class TagNode extends cc.Component {

    @property(cc.Sprite)
    imgSprite: cc.Sprite = null;

    @property(cc.Label)
    textLabel:cc.Label = null;

    @property({tooltip: '是否是选中的标签'})
    isSelected:boolean = false;

    @property({type: TagData, visible(){
        return false;
    }})
    tagData:TagData = null;

    // LIFE-CYCLE CALLBACKS:

    start () {
        //cc.log('TagNode start');
        if(!this.imgSprite) {
            let node = this.node.getChildByName('tag_img');
            if(node) {
                this.imgSprite = node.getComponent(cc.Sprite);
            }
            
        }
        if(!this.textLabel) {
            let node = this.node.getChildByName('tag_text');
            if(node) {
                this.textLabel = node.getComponent(cc.Label);
            }
        }

        if(this.tagData) {
            if(this.imgSprite) {
                this.imgSprite.spriteFrame = this.isSelected ? (this.tagData.selectFrame || this.tagData.spriteFrame): this.tagData.spriteFrame;
            } 
            if(this.textLabel) {
                this.textLabel.string = this.tagData.text;
            }
        }
    }

    setData(tagData:TagData) {
        //cc.log('TagNode setData');
        this.tagData = tagData;
        if(this.imgSprite) {
            this.imgSprite.spriteFrame = this.isSelected ? (this.tagData.selectFrame || this.tagData.spriteFrame) : this.tagData.spriteFrame;
        } 
        if(this.textLabel) {
            this.textLabel.string = this.tagData.text;
        }
    }

    // update (dt) {}
}
